﻿using System;
using System.Windows;
using System.Windows.Controls;
using Airborne.ScrumPoker.Module.Events;

namespace Airborne.ScrumPoker.Module.ProductOwner.Views
{
    /// <summary>
    /// Interaction logic for TimerDetailView.xaml
    /// </summary>
    public partial class TimerDetailView : UserControl, ITimerDetailView
    {
        public TimerDetailView()
        {
            InitializeComponent();
        }

        private void buttonStartTimerClick(object sender, RoutedEventArgs e)
        {
            CountdownTimer timer = Timer;
            if (timer.IsNotNull())
            {
                RaiseStartTimer(timer.CurrentCount);
            }
        }

        private void buttonStopTimerClick(object sender, RoutedEventArgs e)
        {
            RaiseStopTimer();
        }

        private void buttonResetVoteClick(object sender, RoutedEventArgs e)
        {
            RaiseResetVote();
        }

        private void buttonEndGameClick(object sender, RoutedEventArgs e)
        {
            RaiseEndGame();
        }

        #region ITimerDetailView Members

        #region StartTimer

        public event EventHandler<GenericEventArgs<int>> StartTimer;

        private void RaiseStartTimer(int seconds)
        {
            GenericEventArgs<int> args = new GenericEventArgs<int>(seconds);
            OnStartTimer(args);
        }

        protected virtual void OnStartTimer(GenericEventArgs<int> args)
        {
            EventHandler<GenericEventArgs<int>> handler = this.StartTimer;
            if (handler != null)
            {
                handler(this, args);
            }
        }

        #endregion

        #region StopTimer

        public event EventHandler<EventArgs> StopTimer;

        private void RaiseStopTimer()
        {
            EventArgs args = new EventArgs();
            OnStopTimer(args);
        }

        protected virtual void OnStopTimer(EventArgs args)
        {
            EventHandler<EventArgs> handler = this.StopTimer;
            if (handler != null)
            {
                handler(this, args);
            }
        }

        #endregion

        #region ResetVote

        public event EventHandler<EventArgs> ResetVote;

        private void RaiseResetVote()
        {
            EventArgs args = EventArgs.Empty;
            OnResetVote(args);
        }

        protected virtual void OnResetVote(EventArgs args)
        {
            EventHandler<EventArgs> handler = this.ResetVote;
            if (handler != null)
            {
                handler(this, args);
            }
        }

        #endregion

        #region EndGame

        public event EventHandler<EventArgs> EndGame;

        private void RaiseEndGame()
        {
            EventArgs args = EventArgs.Empty;
            OnEndGame(args);
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "EndGame", Justification = "Actual meaning is to End a Game.")]
        protected virtual void OnEndGame(EventArgs args)
        {
            EventHandler<EventArgs> handler = this.EndGame;
            if (handler != null)
            {
                handler(this, args);
            }
        }

        #endregion

        public CountdownTimer Timer
        {
            get
            {
                return DataContext as CountdownTimer;
            }
            set
            {
                DataContext = value;
            }
        }

        #endregion
    }
}
